package gl.web.apk.controller;


import gl.web.apk.common.model.Apk;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.log.Log;

public class ApkController extends Controller {
	public void index() {
		final String packname = getPara("packname");
		int pageindex = getParaToInt("pageindex",1);
		int pageSize= getParaToInt("pagesize",30);
		if (packname != null) {
			setAttr("page", Apk.dao.paginate(pageindex, pageSize, "select *","from apk where packname = ? order by uploaddate desc",packname));
		}else{
			setAttr("page", Apk.dao.paginate(pageindex, pageSize, "select * from apk  order by uploaddate desc",""));
		}
		render("apk.html");
	}
	
	public void add() {
	}
	
	@Before(ApkValidator.class)
	public void save() {
		final Apk apk = getModel(Apk.class);
		apk.setUploaddate(new Date());
		apk.save();
		redirect("/apk");
	}
	
	public void updateurl(){
		String filename  =getPara("filename");
		final Apk apk = Apk.dao.findById(getParaToInt("id"));
		final String url = "/download?filename="+filename;
		apk.setUrl(url);
		apk.setUploaddate(new Date());
		apk.update();
		renderText(url);
	}
	public void edit() {
		Log.getLog("").info("para:"+getPara());
		setAttr("apk", Apk.dao.findById(getParaToInt()));
	}
	
	@Before(ApkValidator.class)
	public void update() {
		getModel(Apk.class).update();
		redirect("/apk");
	}
	
	public void delete() {
		Log.getLog("").debug("para:"+getPara());
		Apk.dao.deleteById(getParaToInt());
		redirect("/apk");
	}
	public void patch() {
		Log.getLog("").info("para:"+getPara());
		renderError(404);
//		redirect("/apk");
	}
	public void api(){
		final String packname = getPara("packname","");
		List<Apk> list;
		if (packname.length()==0) {
			list = Apk.dao.find("select * from apk order by uploaddate desc");
		}else{
			list = Apk.dao.find("select * from apk where packname = ? order by uploaddate desc",packname);
		}
		renderJson(list);
	}
}


